import lrcapi from '@/api/lrcapi'
const formatlrc = (txt) => {
  let oldArr = txt.split(/\n/g)
  let resArr = [] // 最终
  oldArr.forEach((item) => {
    if (item.match(/\[\d+:\d+\.\d+\]/g) === null) {
      return
    }
    // 匹配出数字标签
    item.match(/\[\d+:\d+\.\d+\]/g).forEach((itemb) => {
      let numArr = itemb.replace(/\[|\]/g, '').match(/\d+/g)
      let timeStamp = parseInt(numArr[0]) * 60 + parseInt(numArr[1]) + ((parseInt(numArr[2]) > 50) ? 1 : 0)
      resArr.push({
        timeStamp: timeStamp,
        txt: item.replace(/\[\d+:\d+\.\d+\]/g, '')
      })
    })
  })

  return resArr.sort((a, b) => { return a.timeStamp - b.timeStamp })
}

const lrcModuleObj = {
  state: {
    lrcArray: []
  },
  actions: {
    async getLrc ({commit}, url) {
      let data = await lrcapi.get(url)
      commit('formatLrca', data)
    }
  },
  mutations: {
    formatLrca (state, payload) {
      console.log(payload)
      console.log(formatlrc(payload))
      state.lrcArray = formatlrc(payload)
    }
  }
}

export default lrcModuleObj
